{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "from IPython.display import display, Math\n",
    "from sympy.physics.hydrogen import*\n",
    "# from sympy import init_printing\n",
    "from sympy.abc import *\n",
    "def out(x):\n",
    "   display(Math(latex(x)))\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sympy import init_printing\n",
    "# from IPython.display import display, Math\n",
    "#mu,lambd,delta=symbols('\\mu,\\lambda,\\delta')\n",
    "#参数传入 函数，积分变量和范围\n",
    "#x1=solve([f,g], [x, y, z])\n",
    "#integrate(f, (x, 0, 1),(y,x,x**(1/3)))\n",
    "#simplify(g)\n",
    "# .evalf(subs ={'x2':6})\n",
    "#def out(x):\n",
    "   # display(Math(latex(sympify(x))))\n",
    "# def out(x,y):\n",
    "#    display(Math(latex(sympify(x))))\n",
    "#    if y==1:\n",
    "#     print(latex(sympify(x)))\n",
    "# @handcalc(left=\"\",right=\"\",jupyter_display=True)\n",
    "# def NormalDistribution(x, y):\n",
    "#    y=sqrt(y)\n",
    "#    t=sqrt(2*pi)\n",
    "#    m=exp() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{2} - x - 6$"
      ],
      "text/plain": [
       "x**2 - x - 6"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x + 2)*(x - 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left(x - 1\\right) \\left(x^{2} + 1\\right)$"
      ],
      "text/plain": [
       "(x - 1)*(x**2 + 1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**3 - x**2 + x - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, [(z, 1), (x + 2*y, 2)])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_list(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "collect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{3} - x^{2} z + 2 x^{2} + x y + x - 3$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{3} + x^{2} \\left(2 - z\\right) + x \\left(y + 1\\right) - 3$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 - z$"
      ],
      "text/plain": [
       "2 - z"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3\n",
    "out(expr)\n",
    "collected_expr=collect(expr,x)\n",
    "out(collected_expr)\n",
    "collected_expr.coeff(x, 2)#取除次方项"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{x y^{2} - 2 x y z + x z^{2} + y^{2} - 2 y z + z^{2}}{x^{2} - 1}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{y^{2} - 2 y z + z^{2}}{x - 1}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\left(y - z\\right)^{2}}{x - 1}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)\n",
    "out(expr)\n",
    "out(cancel(expr))\n",
    "out(factor(expr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分数拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{4 x^{3} + 21 x^{2} + 10 x + 12}{x^{4} + 5 x^{3} + 5 x^{2} + 4 x}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{2 x - 1}{x^{2} + x + 1} - \\frac{1}{x + 4} + \\frac{3}{x}$"
      ],
      "text/plain": [
       "(2*x - 1)/(x**2 + x + 1) - 1/(x + 4) + 3/x"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)\n",
    "out(expr)\n",
    "expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)\n",
    "apart(expr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三角化简，展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\cos{\\left(4 x \\right)}}{2} + \\frac{1}{2}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{2 \\tan{\\left(x \\right)}}{1 - \\tan^{2}{\\left(x \\right)}}$"
      ],
      "text/plain": [
       "2*tan(x)/(1 - tan(x)**2)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "usee_x=trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)\n",
    "out(usee_x)\n",
    "expand_trig(tan(2*x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "组合化简"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{n + 1}{k + 1}$"
      ],
      "text/plain": [
       "(n + 1)/(k + 1)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combsimp(binomial(n+1, k+1)/binomial(n, k))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求导"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 4 x^{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 16 x^{3}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 96 x$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\partial^{7}}{\\partial z^{4}\\partial y^{2}\\partial x} e^{x y z}=x^{3} y^{2} \\left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\\right) e^{x y z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f=4*x**4\n",
    "out(f)\n",
    "out(diff(f,x))\n",
    "out(diff(f,x,3))\n",
    "expr = exp(x*y*z)\n",
    "Math(latex(Derivative(expr,x,y,2,z,4))+'='+latex(diff(expr, x, y, 2, z, 4)))#.doit() 计算\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\int \\log{\\left(x \\right)}^{2}\\, dx=x \\log{\\left(x \\right)}^{2} - 2 x \\log{\\left(x \\right)} + 2 x$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = Integral(log(x)**2, x)\n",
    "Math(latex(expr)+'='+latex(expr.doit()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "泰勒展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{x^{2}}{2} + x + 1$"
      ],
      "text/plain": [
       "x**2/2 + x + 1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = exp(sin(x))\n",
    "expr.series(x, 0, 4).removeO()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方程组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{2 n \\pi + \\frac{\\pi}{2}\\; \\middle|\\; n \\in \\mathbb{Z}\\right\\}$"
      ],
      "text/plain": [
       "ImageSet(Lambda(_n, 2*_n*pi + pi/2), Integers)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solveset(sin(x) - 1, x, domain=Reals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( - y - 1, \\  y, \\  2\\right)\\right\\}$"
      ],
      "text/plain": [
       "{(-y - 1, y, 2)}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{x^{2}}{2} + \\frac{x^{3}}{3} i + 3 x j + 4 x k$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 12 + \\frac{124}{3} i + 12 j + 16 k$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left(- 2 x^{2} + x - 25\\right) + \\left(x^{2} + 2 x\\right) i + \\left(- 4 x^{2} + 3 x + 11\\right) j + \\left(3 x^{2} + 4 x - 2\\right) k$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left( \\frac{x^{2}}{\\sqrt{- \\frac{x^{2}}{x^{4} + x^{2} + 25} + 1} \\sqrt{x^{4} + x^{2} + 25}}, \\  \\frac{3}{\\sqrt{- \\frac{x^{2}}{x^{4} + x^{2} + 25} + 1} \\sqrt{x^{4} + x^{2} + 25}}, \\  \\frac{4}{\\sqrt{- \\frac{x^{2}}{x^{4} + x^{2} + 25} + 1} \\sqrt{x^{4} + x^{2} + 25}}\\right)$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 \\operatorname{acos}{\\left(\\frac{x}{\\sqrt{x^{4} + x^{2} + 25}} \\right)}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a=Quaternion(x, x**2, 3, 4)\n",
    "b=Quaternion(1, 2, 3, 4)\n",
    "out(a.integrate(x))#定积分\n",
    "out(a.integrate((x, 1, 5)))#不定积分\n",
    "out(a.mul(b))#乘法\n",
    "(axis, angle) = a.to_axis_angle()# 轴线和转角\n",
    "out(axis)\n",
    "out(angle)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 物理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.physics.vector import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\sqrt{2}}{2}\\mathbf{\\hat{i}_x} + \\frac{\\sqrt{2}}{2}\\mathbf{\\hat{i}_y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{2}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "N = ReferenceFrame('i')\n",
    "out((N.x + N.y).normalize())\n",
    "out((N.x + N.y).magnitude())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{40}{3}$"
      ],
      "text/plain": [
       "-40/3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate((2-r**2)*r*(-8-8*r**2),(r,0,sqrt(2)))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6b2a446df440bee9c99ca0d62e47ab197710c7f47ecf8f74e850abc8bd383c6"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
